package com.yq.interview.study.thread;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @ClassName ProdConsumer_BlockingQueueDemo
 * @Description demo
 * volatitle/CAS/atomicInteger/BlockingQueue/线程交互/原子引用
 * <p>
 * 传入true
 * 生产一个消费一个--模拟阻塞队列
 * @Author yaoqiang
 * @Date 2020/11/23 13:58
 * @Version 1.0
 */
public class ProdConsumer_BlockingQueueDemo {
    public static void main(String[] args) {
        //线程操作资源类
        MyResource myResource = new MyResource(new ArrayBlockingQueue<>(10));
        new Thread(() -> {
            System.out.println(Thread.currentThread().getName() + "\t生产启动");
            try {
                myResource.myProd();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }, "prod").start();

        new Thread(() -> {
            System.out.println(Thread.currentThread().getName() + "\t消费启动");
            try {
                myResource.myConsumer();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }, "consumer").start();
        try {
            TimeUnit.SECONDS.sleep(5);
            System.out.println("sleep5");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("..........停");
        myResource.stop();
    }
}

class MyResource {
    private volatile boolean FLAG = true;//默认开启生产加消费,保证可见性，不保证原子性，每个线程能获取到该变量的最新值，
    private AtomicInteger atomicInteger = new AtomicInteger();//多线程不要用i++之类的，用atomicInteger.atomicInteger.incrementAndGet()
    BlockingQueue<String> blockingQueue = null;

    //传接口，不要传具体,写往高处写，查往细查
    public MyResource(BlockingQueue<String> blockingQueue) {
        this.blockingQueue = blockingQueue;
        System.out.println(blockingQueue.getClass().getName());
    }

    public void myProd() throws Exception {
        String data = null;
        boolean returnValue;
        while (FLAG) {
            data = atomicInteger.incrementAndGet() + "";
            returnValue = blockingQueue.offer(data, 2L, TimeUnit.SECONDS);
            if (returnValue) {
                System.out.println(Thread.currentThread().getName() + "\t插入队列成功数据是" + data);
            } else {
                System.out.println(Thread.currentThread().getName() + "\t插入队列数据是" + data + "...............false");
            }
            TimeUnit.SECONDS.sleep(2);
        }
        System.out.println(Thread.currentThread().getName() + "====================生产结束");
    }


    public void myConsumer() throws Exception {
        String result = null;
        boolean returnValue;
        while (FLAG) {
            result = blockingQueue.poll(2L, TimeUnit.SECONDS);
            if (null == result || result.equalsIgnoreCase("")) {
                FLAG = false;
                System.out.println(Thread.currentThread().getName() + "\t超过两秒");
            }
            System.out.println(Thread.currentThread().getName() + "\t取到的数据是" + result);

        }
        System.out.println(Thread.currentThread().getName() + "====================消费结束");
    }

    public void stop() {
        FLAG = false;
    }
}
